package emulator.hardware.nmos6502.operands;

import emulator.assembler.SymbolTable;
import emulator.hardware.HwWord;
import emulator.hardware.debug.BusWatchException;
import emulator.hardware.memory.UnmappedMemoryException;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.util.MemoryPtr;

public class AbsoluteY extends ByteOperand {

	private boolean add_page_crossing = true;
	private HwWord abs_addr = new HwWord();

	public AbsoluteY() { super(); }
	public AbsoluteY(boolean add_page_crossing)
	{ 
		super();
		this.add_page_crossing = add_page_crossing;
	}
	
	@Override
	public void init(Cpu6502 cpu) throws UnmappedMemoryException, BusWatchException {
		super.init(cpu);
		HwWord addr = new HwWord(cpu.fetchProgramWord());
		abs_addr.setNumber(addr.getNumber());
		addr.add(cpu.getY());
		setAddress(addr);
	}

	@Override
	public String getString() {
		return "$"+abs_addr+",Y";
	}

	@Override
	public int getByteCount() {
		return 2;
	}

	@Override
	public String getString(MemoryPtr mem_ref, SymbolTable symbols) {
		return symbols.getLabelOrAddress(mem_ref.getWordOp())+",Y";
	}

	@Override
	public int getExtraCycles() {
		if (add_page_crossing &&
				!abs_addr.hi().equals(getAddress().hi()))
			return 3;
		return 2;
	}
}
